<?php
namespace App\Http\Controllers\Backstage;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Validator\DepartmentVali;
use App\Model\Backstage\Department;
use App\Model\Backstage\User;
use App\ServiceHandler\Common\DepartmentService;
use App\Tool\Error;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Validator;

/**
 * Class DepartmentController
 * @package App\Http\Controllers\Backstage
 * 公司对应部门控制器
 */
class DepartmentController extends Controller
{
    protected $department;
    public function __construct()
    {
        $this->department = new Department();
    }

    /**
     * 获取该中介公司系统组的信息
     */
    public function getSystemInfo()
    {
        return $this->success('ok', $this->department->getSystemInfo());
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 获取部门列表，如果传公司ID 获取本公司部门
     */
    public function getDepartmentList (Request $request)
    {
        $map = [];
        $name = trim($request->input('name')); //部门名称检索
        isset($name)?$map[] = ['name', 'like', "%".$name."%"]:'';
        $d_superior_id = '';
        if ($request->input('d_superior_id')) {
            $d_superior_id = $request->input('d_superior_id');
        }
        $list = $this->department->getDepartmentList(env('PAGE_LIMIT_ADMIN'),$map, $d_superior_id);
        if (!$list){
            return $this->error(Error::NULL_DATA);
        }
        return $this->success('ok',$list);
    }


    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 添加部门信息
     */
    public function addDepartment(Request $request)
    {
        $params = $request->except('token');
        $vali = DepartmentVali::validate($params);
        if(!$vali){
            return $this->error(Error::PARAM_MISSING);
        }
        if (!$request->input('d_superior_id')){
            $params['d_superior_id'] =  $this->department->getSystemId();
        }
        $res = $this->department->addDepartment($params);
        if($res){
            return $this->success('ok','添加成功！');
        }else{
            return $this->error(Error::ADD_ERROR);
        }

    }


    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 修改部门信息
     */
    public function editDepartment (Request $request)
    {
        if (!$request->has('id')){
            return $this->error(Error::PARAM_MISSING);
        }
        $params = $request->except('token', 'd_superior_id');
        $vali = DepartmentVali::validate($params);
        if(!$vali){
            return $this->error(Error::PARAM_MISSING);
        }
        $res = $this->department->saveDepartment($params);
        if($res){
            return $this->success('ok','修改成功！');
        }else{
            return $this->error(Error::EDIT_ERROR);
        }
    }

    /**
     * 修改部门的权限
     */
    public function editDepartmentPermission(Request $request)
    {
        $params = $request->except('token');
        $vali = DepartmentVali::editPermission($params);
        if(!$vali){
            return $this->error(Error::PARAM_MISSING);
        }
        $id = Department::editDepartmentPermission($params);
        if (!$id) {
            return $this->error(Error::EDIT_ERROR);
        }
        return $this->success('ok','修改成功！');
    }

    /**
     * 修改了部门权限以后   在批量修改该部门下面所有员工的权限
     */
    public function editDepartmentDownUserPermission(Request $request)
    {
        if (!$request->input('id')){
            return $this->error(Error::PARAM_MISSING);
        }
        $res = Department::editDepartmentDownUserPermission($request->input('id'));
        if ($res === -1) {
            return $this->error(Error::DEPARTMENT_NO_USER);
        }
        if (!$res) {
            return $this->error(Error::EDIT_ERROR);
        }
        return $this->success('ok','修改成功！');

    }


    /**
     * @param Request $request
     * @param User $user
     * @return \Illuminate\Http\JsonResponse
     * 删除部门信息 逻辑删除  需要判断部门下面是否有用户
     */
    public function delDepartment (Request $request)
    {
        $id = $request->input('id');
        if(!$id){
            return $this->error(Error::PARAM_MISSING);
        }
        $res = $this->department->delDepartment($id);
        if($res['code'] == 0){
            return $this->success('ok','操作成功！');
        }else{
            return $this->error(1,$res['msg']);
        }
    }

    /**回收站彻底删除部门
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function recycleDelDepartment (Request $request)
    {
        $id = $request->input('id');
        if(!$id){
            return $this->error(Error::PARAM_MISSING);
        }
        $res = $this->department->recycleDelDepartment($id);
        if (!$res) {
            return $this->error(Error::DEL_ERROR);
        }
        return $this->success('ok', '删除成功！');
    }

    /**
     * 根据个人权限获取中介公司下部门下拉框
     */
    public function dropdown(Request $request)
    {
        //  type= 所有 4 跨部3 本部2 本人1
        if (!$request->input('type')) {
            return $this->error(Error::PARAM_MISSING);
        }
        $list = DepartmentService::getByType($request->input('type'));
        if (!$list){
            return $this->error(Error::NULL_DATA);
        }
        return $this->success('ok',$list);
    }

    /**
     * 根据部门的ID   获取该部门下面的员工下拉框内容
     */
    public function userDropdown(Request $request)
    {
        $map = [];
        $dId = $request->input('d_id');
        if($dId != 0) {
            $map['u_d_id'] = $dId;
        }
        $list  = $this->department->downList($map);
        if (sizeof($list) == 0) {
            return $this->error(Error::NULL_DATA);
        }
        return $this->success('ok', $list);
    }

    //部门回收站列表
    public function RecycleList(Request $request,Department $Department){
        $param = $request->all();
        $where[] = ['c_id',Auth::user()['u_b_id']];
        if($request->input('start_time')){
            $where[] = ['department.updated_at', '>=', strtotime($param['start_time'].' 00:00:00')];
        } else {
            $where[] = ['department.updated_at', '>=', strtotime(date('Y-m-d',strtotime('-3 month')).' 00:00:00')];
        }

        if($request->input('end_time')){
            $where[] = ['department.updated_at', '<=', strtotime($param['end_time'].' 23:59:59')];
        } else {
            $where[] = ['department.updated_at', '<=', time()];
        }

        //关键词智能搜索
        $keywords = '';
        if ($request->input('keywords')){
            //部门名称 部门电话
            $keywords = trim($request->input('keywords'));
        }

        $list = $Department->recycleBinList(env('PAGE_LIMIT_ADMIN'),$where,$keywords);
        if (!$list) {
            $data['start'] = date('Y-m-d',strtotime('-3 month'));
            $data['end']  = date('Y-m-d',time());
            return $this->error(Error::NULL_DATA,$data);
        }
        $start = date('Y-m-d',strtotime('-3 month'));
        $end = date('Y-m-d',time());
        return $this->success('ok', $list,$start,$end);
    }



}